<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;



use app\common\corelib\CacheHelper;
use app\models\Answer;
use app\models\Chapter;
use app\models\Classes;
use app\models\Comment;
use app\models\Course;
use app\models\History;
use app\models\Paper;
use app\models\PaperTest;
use app\models\Sentence;
use app\models\Test;
use app\models\User;

class CourseLogic
{

    public function getById($id)
    {
        return Course::find()->where(['course_id' => $id])->asArray()->one();
    }

    public function getClass(){
        return Classes::find()->where(['is_del'=>0])->orderBy('class_order')->asArray()->all();
    }

    public function getCourse($course_type,$extend_type){
        $course = Course::find()->where(['course_type'=>$course_type,'course_status'=>1,'is_del'=>0]);
        if($course_type == 2 || $course_type == 3 || $course_type == 5 || $course_type == 6){
            $course->andWhere(['extend_type'=>$extend_type]);
        }
        return $course->asArray()->one();
    }

    public function getChapter($course_id){
        return Chapter::find()->where(['course_id'=>$course_id,'is_del'=>0])->orderBy('chapter_order')->asArray()->all();
    }

    public function getChapterById($chapter_id){
        return Chapter::find()->where(['chapter_id'=>$chapter_id])->asArray()->one();
    }

    public function getComment($course_id,$page=1,$limit=10,&$count){
        $comment = Comment::find()
            ->select('c.*,u.nick_name,u.head_img')
            ->from('{{%comment}} c')
            ->leftJoin('{{%user}} u','u.user_id=c.user_id')
            ->where(['c.course_id'=>$course_id,'is_del'=>0])->orderBy('add_time desc');
        $count = $comment->count();
        return $comment->offset($limit*($page-1))->limit($limit)->asArray()->all();
    }

    public function addComment($open_id,$course_id,$comment_score,$comment_content){
        $user = User::find()->where(['open_id'=>$open_id])->asArray()->one();
        $comment = new Comment();
        $comment->course_id = $course_id;
        $comment->user_id = $user['user_id'];
        $comment->comment_socre = $comment_score;
        $comment->comment_content = $comment_content;
        $comment->add_time = date('Y-m-d');
        $comment->save();

    }

    public function getPaper($paper_id){
        return Paper::find()->where(['paper_id'=>$paper_id])->asArray()->one();
    }

    public function getSentence($sentence_type,$sentence_extend_type=0){
        if($sentence_extend_type){
            $sentence = Sentence::find()->select('sentence_id')->where(['sentence_type'=>$sentence_type,'sentence_extend_type'=>$sentence_extend_type,'is_del'=>0])->asArray()->all();
        }else{
            $sentence = Sentence::find()->select('sentence_id')->where(['sentence_type'=>$sentence_type,'is_del'=>0])->asArray()->all();
        }

        $ids = [];
        foreach($sentence AS $value){
            $ids[] = $value['sentence_id'];
        }
        $rand_key = array_rand($ids,1);
        $id = $ids[$rand_key];
        return Sentence::find()->where(['sentence_id'=>$id])->asArray()->one();
    }

    public function getCorrect($user_id,$paper_id,$answer_no){
        return Answer::find()
            ->select('sum(is_correct) as correct')
            ->where("user_id=$user_id AND paper_id=$paper_id AND answer_no='$answer_no'")
            ->groupBy('user_id,paper_id,test_id')
            ->having('correct=1')
            ->count();
    }
    public function getNotCorrect($user_id,$paper_id,$answer_no){
        return Answer::find()
            ->select('sum(is_correct) as correct')
            ->where("user_id=$user_id AND paper_id=$paper_id AND answer_no='$answer_no'")
            ->groupBy('user_id,paper_id,test_id')
            ->having('correct=0')
            ->count();
    }

    public function getPaperTitle($paper_id){
        $paper = Paper::find()
            ->select('p.*,c.chapter_name,co.course_name')
            ->from('{{%paper}} p')
            ->leftJoin('{{%chapter}} c','c.paper_id=p.paper_id')
            ->leftJoin('{{%course}} co','co.course_id=c.course_id')
            ->where(['p.paper_id'=>$paper_id])->asArray()->one();
        if($paper['course_name'].$paper['chapter_name'])
            return $paper['course_name'].$paper['chapter_name'];
        else
            return $paper['paper_name'];
    }

    /*public function getTest($paper_type,$chapter_id,$paper_id,$test_id=0){
        if($paper_id){
            if($test_id==0){
                $res = Test::find()
                    ->from('{{%test}} t')
                    ->leftJoin('{{%paper_test}} pt','pt.test_id=t.test_id')
                    ->where(['pt.paper_id'=>$paper_id])
                    ->asArray()
                    ->one();
            }else{
                $res = Test::find()
                    ->from('{{%test}} t')
                    ->leftJoin('{{%paper_test}} pt','pt.test_id=t.test_id')
                    ->where(['pt.paper_id'=>$paper_id])
                    ->andWhere("test_id>$test_id")
                    ->asArray()
                    ->one();
            }
        }elseif($chapter_id){
            if($test_id==0){
                $res = Test::find()
                    ->from('{{%test}} t')
                    ->leftJoin('{{%paper}} p','p.paper_id=t.paper_id')
                    ->leftJoin('{{%chapter}} c','c.chapter_id=p.chapter_id')
                    ->where(['chapter_id'=>$chapter_id])
                    ->asArray()
                    ->one();
            }else{
                $res = Test::find()
                    ->from('{{%test}} t')
                    ->leftJoin('{{%paper}} p','p.paper_id=t.paper_id')
                    ->leftJoin('{{%chapter}} c','c.chapter_id=p.chapter_id')
                    ->where(['chapter_id'=>$chapter_id])
                    ->andWhere("test_id>$test_id")
                    ->asArray()
                    ->one();
            }
        }elseif($paper_type){
            if($test_id==0){
                $res = Test::find()
                    ->from('{{%test}} t')
                    ->leftJoin('{{%paper}} p','p.paper_id=t.paper_id')
                    ->where(['p.paper_type'=>$paper_type])
                    ->asArray()
                    ->one();
            }else{
                $res = Test::find()
                    ->from('{{%test}} t')
                    ->leftJoin('{{%paper}} p','p.paper_id=t.paper_id')
                    ->where(['p.paper_type'=>$paper_type])
                    ->andWhere("test_id>$test_id")
                    ->asArray()
                    ->one();
            }
        }
        return $res;
    }*/

    public function getTest($paper_id,$test_id=0,$answer_no='',$open_id='',$reset=false){
        if($paper_id==1 || $paper_id==2){
            if($test_id==0){
                if($paper_id==1){
                    if($reset)
                        CacheHelper::del('paper1_'.$open_id);
                    $paper1 = CacheHelper::get('paper1_'.$open_id);
                    if(!$paper1){
                        $restemp = Test::find()
                            ->from('{{%test}} t')
                            ->leftJoin('{{%paper_test}} pt','pt.test_id=t.test_id')
                            ->where(['pt.paper_id'=>$paper_id,'t.is_del'=>0])
                            ->asArray()
                            ->all();
                        shuffle($restemp);
                        $i1 = $i2  = $i3 = $i4 = 0;
                        foreach($restemp AS $key=>$value){
                            if($value['test_extend']==1 && $i1<5){
                                $res[] = $value;
                                $i1++;
                            }
                            elseif($value['test_extend']==2 && $i2<5){
                                $res[] = $value;
                                $i2++;
                            }elseif($value['test_extend']==3 && $i3<5){
                                $res[] = $value;
                                $i3++;
                            }elseif($value['test_extend']==4 && $i4<5){
                                $res[] = $value;
                                $i4++;
                            }
                        }
                        CacheHelper::set('paper1_'.$open_id,$res);
                        return $res[0];
                    }else{
                        return $paper1[0];
                    }
                }
                elseif($paper_id==2){
                    if($reset)
                        CacheHelper::del('paper2_'.$open_id);
                    $paper2 = CacheHelper::get('paper2_'.$open_id);
                    if(!$paper2){
                        $restemp = Test::find()
                            ->from('{{%test}} t')
                            ->leftJoin('{{%paper_test}} pt','pt.test_id=t.test_id')
                            ->where(['pt.paper_id'=>$paper_id,'t.is_del'=>0])
                            ->asArray()
                            ->all();
                        shuffle($restemp);
                        $i5 = $i6 = $i7 = 0;
                        foreach($restemp AS $key=>$value){
                            if($value['test_extend']==5 && $i5<6){
                                $res[] = $value;
                                $i5++;
                            }elseif($value['test_extend']==6 && $i6<6){
                                $res[] = $value;
                                $i6++;
                            }elseif($value['test_extend']==7 && $i7<8){
                                $res[] = $value;
                                $i7++;
                            }
                        }
                        CacheHelper::set('paper2_'.$open_id,$res);
                        return $res[0];
                    }else{
                        return $paper2[0];
                    }
                }
            }else{
                if($paper_id==1){
                    $paper1 = CacheHelper::get('paper1_'.$open_id);
                    $isexit = Answer::find()->where(['answer_no'=>$answer_no])->asArray()->one();
                    //var_dump($answer_no);exit;
                    if(!$isexit){
                        return $paper1[0];
                    }
                    foreach($paper1 AS $key=>$value){
                        if($value['test_id']==$test_id)
                            return $paper1[$key+1];
                    }
                }elseif($paper_id==2){
                    $paper2 = CacheHelper::get('paper2_'.$open_id);
                    $isexit = Answer::find()->where(['answer_no'=>$answer_no])->asArray()->one();
                    if(!$isexit){
                        return $paper2[0];
                    }
                    foreach($paper2 AS $key=>$value){
                        if($value['test_id']==$test_id)
                            return $paper2[$key+1];
                    }
                }
            }
        }else{
            if($test_id==0){
                $res = Test::find()
                    ->from('{{%test}} t')
                    ->leftJoin('{{%paper_test}} pt','pt.test_id=t.test_id')
                    ->where(['pt.paper_id'=>$paper_id,'t.is_del'=>0])
                    ->orderBy('pt.sort,t.test_id')
                    ->asArray()
                    ->one();
            }else{
                $answer = Answer::find()->where(['answer_no'=>$answer_no])->asArray()->all();
                $test_ids = [];
                foreach($answer AS $key=>$value){
                    $test_ids[] = $value['test_id'];
                }
                $sort = PaperTest::find()->where(['paper_id'=>$paper_id,'test_id'=>$test_id])->asArray()->one();
                $res = Test::find()
                    ->from('{{%test}} t')
                    ->leftJoin('{{%paper_test}} pt','pt.test_id=t.test_id')
                    ->where(['pt.paper_id'=>$paper_id,'t.is_del'=>0])
                    ->andWhere("pt.sort>=".$sort['sort'])
                    ->andWhere(['not in','t.test_id',$test_ids])
                    ->orderBy('pt.sort,t.test_id')
                    ->asArray()
                    ->one();
            }
        }

        return $res;
    }

    public function getAllTest($paper_id,$open_id=''){
        if($paper_id==1 || $paper_id==2){
            if($paper_id==1){
                $paper1 = CacheHelper::get('paper1_'.$open_id);
                return $paper1;
            }elseif($paper_id==2){
                $paper2 = CacheHelper::get('paper2_'.$open_id);
                return $paper2;
            }
        }else {
            return Test::find()
                ->from('{{%test}} t')
                ->leftJoin('{{%paper_test}} pt', 'pt.test_id=t.test_id')
                ->where(['pt.paper_id' => $paper_id, 't.is_del' => 0])->orderBy('pt.sort asc,t.test_id asc')->asArray()->all();
        }
    }

    public function getLastTest($paper_id,$open_id=''){
        if($paper_id==1 || $paper_id==2){
            if($paper_id==1){
                $paper1 = CacheHelper::get('paper1_'.$open_id);
                return end($paper1);
            }elseif($paper_id==2){
                $paper2 = CacheHelper::get('paper2_'.$open_id);
                return end($paper2);
            }
        }else{
            return Test::find()
                ->from('{{%test}} t')
                ->leftJoin('{{%paper_test}} pt','pt.test_id=t.test_id')
                ->where(['pt.paper_id'=>$paper_id,'t.is_del'=>0])->orderBy('pt.sort desc,t.test_id desc')->asArray()->one();
        }

    }

    public function getAnswerByOpenId($open_id,$paper_id){
        return Answer::find()
            ->from('{{%answer}} a')
            ->leftJoin('{{%user}} u','u.user_id=a.user_id')
            ->leftJoin('{{%paper_test}} p','p.test_id=a.test_id')
            ->where(['u.open_id'=>$open_id,'p.paper_id'=>$paper_id])
//            ->andWhere(['or','a.is_correct'=>1,'a.answer_num'=>2])
            ->orderBy('a.answer_time desc')
            ->asArray()
            ->one();
    }

    public function getTestById($test_id){
        return Test::find()->where(['test_id'=>$test_id])->asArray()->one();
    }

    public function answer($user_id,$test_id,$answer,$paper_id,$is_correct,$answer_no){
        $old_answer = Answer::find()->where(['user_id'=>$user_id,'test_id'=>$test_id,'paper_id'=>$paper_id,'answer_no'=>$answer_no])->orderBy('answer_time desc')->asArray()->one();
        $new_answer = new Answer();
        $new_answer->user_id = $user_id;
        $new_answer->test_id = $test_id;
        $new_answer->answer = $answer;
        $new_answer->paper_id = $paper_id;
        $new_answer->is_correct = $is_correct;
        $new_answer->answer_no = $answer_no;
        $new_answer->answer_time = date('Y-m-d H:i:s');
        if(!$old_answer){
            $new_answer->answer_num = 1;
            $new_answer->save();
            return 1;
        }elseif($old_answer['answer_num']==1){
            $new_answer->answer_num = 2;
            $new_answer->save();
            return 2;
        }else{
            $new_answer->answer_num = 1;
            $new_answer->save();
            return 1;
        }

    }

    public function getLastChapter($course_id){
        return History::find()
            ->from('{{%history}} h')
            ->leftJoin('{{%chapter}} c','c.chapter_id=h.id')
            ->where(['h.course_id'=>$course_id,'h.type'=>1,'c.is_del'=>0])->orderBy('time desc')->asArray()->one();
    }

}
